﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using RaisingStudio.Xna.Graphics;
using System.Threading.Tasks;
using System.Threading;

namespace WindowsGame1
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game4 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        DrawingComponent drawingComponent;
        SpriteFont spriteFont;
        Texture2D texture;

        public Game4()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            drawingComponent = new DrawingComponent(this);
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            this.Components.Add(this.drawingComponent);

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            spriteFont = Content.Load<SpriteFont>("SegoeUI");
            texture = Content.Load<Texture2D>("Tulips");

            Color[] backgroundColor = Enumerable.Repeat<Color>(Color.White, drawingComponent.DrawingTexture.Width * drawingComponent.DrawingTexture.Height).ToArray();
            drawingComponent.DrawingTexture.Background.SetData<Color>(backgroundColor);
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        private bool mouseDown = false;
        private Vector2 lastMousePosition;

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here
            MouseState mouseState = Mouse.GetState();
            if (mouseDown)
            {
                Vector2 mousePosition = new Vector2(mouseState.X, mouseState.Y);
                if (mousePosition != lastMousePosition)
                {
                    var preMousePosition = lastMousePosition;
                    drawingComponent.DrawingContext.DrawLine(preMousePosition, mousePosition, Color.Black);
                    lastMousePosition = mousePosition;
                }
                if (mouseState.LeftButton == ButtonState.Released)
                {
                    mouseDown = false;
                }
            }
            else
            {
                if (mouseState.LeftButton == ButtonState.Pressed)
                {
                    mouseDown = true;
                    lastMousePosition = new Vector2(mouseState.X, mouseState.Y);
                }
            }

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.White);

            // TODO: Add your drawing code here            

            base.Draw(gameTime);

            spriteBatch.Begin();
            if (gameTime.ElapsedGameTime.TotalSeconds > 0)
            {
                spriteBatch.DrawString(spriteFont, ((1.0f / gameTime.ElapsedGameTime.TotalSeconds).ToString("0fps")), new Vector2(10.0f, 10.0f), Color.Blue);
            }
            spriteBatch.End();
        }

        private System.Random random;

        protected override void BeginRun()
        {
            base.BeginRun();
            Task.Factory.StartNew(() =>
                {
                    this.random = new Random((int)DateTime.Now.Ticks);
                    PresentationParameters pp = GraphicsDevice.PresentationParameters;
                    int bufferWidth = pp.BackBufferWidth;
                    int bufferHeight = pp.BackBufferHeight;

                    DrawingContext drawingContext = this.drawingComponent.DrawingContext;
                    drawingContext.Begin();
                    while (true)
                    {
                        int commandType = this.random.Next(10);
                        switch (commandType)
                        {
                            case 0:
                                {
                                    drawingContext.DrawLine(random.Next(bufferWidth), random.Next(bufferHeight), random.Next(bufferWidth), random.Next(bufferHeight), new Color(random.Next(255), random.Next(255), random.Next(255)));
                                    break;
                                }
                            case 1:
                                {
                                    drawingContext.DrawRectangle(random.Next(bufferWidth), random.Next(bufferHeight), random.Next(bufferWidth), random.Next(bufferHeight), new Color(random.Next(255), random.Next(255), random.Next(255)));
                                    break;
                                }
                            case 2:
                                {
                                    drawingContext.DrawTriangle(random.Next(bufferWidth), random.Next(bufferHeight), random.Next(bufferWidth), random.Next(bufferHeight), random.Next(bufferWidth), random.Next(bufferHeight), new Color(random.Next(255), random.Next(255), random.Next(255)));
                                    break;
                                }
                            case 3:
                                {
                                    drawingContext.DrawEllipse(random.Next(bufferWidth), random.Next(bufferHeight), random.Next(bufferWidth), random.Next(bufferHeight), new Color(random.Next(255), random.Next(255), random.Next(255)));
                                    break;
                                }
                            case 4:
                                {
                                    drawingContext.DrawTexture(texture, new Vector2(random.Next(bufferWidth), random.Next(bufferHeight)), Color.White);
                                    break;
                                }
                            case 5:
                                {
                                    int count = random.Next(10);
                                    if (count > 1)
                                    {
                                        Vector2[] points = new Vector2[count];
                                        for (int i = 0; i < count; i++)
                                        {
                                            points[i] = new Vector2(random.Next(bufferWidth), random.Next(bufferHeight));
                                        }
                                        drawingContext.DrawPolyline(points, new Color(random.Next(255), random.Next(255), random.Next(255)));
                                    }
                                    break;
                                }
                            case 6:
                                {
                                    drawingContext.DrawFilledRectangle(random.Next(bufferWidth), random.Next(bufferHeight), random.Next(bufferWidth), random.Next(bufferHeight), new Color(random.Next(255), random.Next(255), random.Next(255)));
                                    break;
                                }
                            case 7:
                                {
                                    drawingContext.DrawFilledTriangle(random.Next(bufferWidth), random.Next(bufferHeight), random.Next(bufferWidth), random.Next(bufferHeight), random.Next(bufferWidth), random.Next(bufferHeight), new Color(random.Next(255), random.Next(255), random.Next(255)));
                                    break;
                                }
                            case 8:
                                {
                                    drawingContext.DrawFilledEllipse(random.Next(bufferWidth), random.Next(bufferHeight), random.Next(bufferWidth), random.Next(bufferHeight), new Color(random.Next(255), random.Next(255), random.Next(255)));
                                    break;
                                }
                            case 9:
                                {
                                    drawingContext.DrawText(spriteFont, "Hello World!", new Vector2(random.Next(bufferWidth), random.Next(bufferHeight)), Color.Black);
                                    break;
                                }
                        }
                        Thread.Sleep(10);
                    }
                    drawingContext.End();
                });
        }
    }
}